﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Admin;
using OpenSoftStore.BusinessLogic.Member;
using OpenSoftStore.BusinessLogic.Money;
using OpenSoftStore.Client.Common;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Orm.Static;
using OpenSoftStore.Orm.View;

namespace OpenSoftStore.Client.UIL.ManageMember
{
    public partial class MemberReturnMoneyEdit : UserControl
    {
        public MemberLogic LgcMember;
        public T_Member Tmember;
        public List<V_MemberOwed> MemberOweds;
        public T_MemberReturnMoney TblMemberReturnMoney;
        public List<T_MemberReturnMoneyItem> TblMemberReturnMoneyItems;
        public AdminLogic LgcAdmin;
        public Boolean Drog = false;
        public int Top = 0;
        public int Left = 0;

        public MemberReturnMoneyEdit()
        {
            InitializeComponent();
            LgcMember=new MemberLogic();
            dgvMemberOwed.AutoGenerateColumns = false;
            MemberOweds=new List<V_MemberOwed>();
            TblMemberReturnMoney=new T_MemberReturnMoney();
            TblMemberReturnMoneyItems=new List<T_MemberReturnMoneyItem>();
            LgcAdmin=new AdminLogic();

            cbType.Text = "全部业务";
            var admins = LgcAdmin.GetAdminViewFast();
            cmbAdmin.DataSource = admins;
            cmbAdmin.DisplayMember = "Name";
            cmbAdmin.ValueMember = "Id";
            cmbAdmin.Text = S_Admin.Name;
            SPACommon.SetDataGridStyle(dgvMemberOwed);
        }
        private void MemberReturnMoneyEdit_Load(object sender, EventArgs e)
        {
            dgvMemberOwed.Focus();
        }

        public void GetMember(T_Member tmember)
        {
            Tmember = tmember;
            txtMemberId.Text = tmember.CardId + " " + tmember.Name;

            dgvMemberOwed.DataSource = null;
            MemberOweds = LgcMember.GetMemberOwedView(" where (NeedPay - CashPay - BankPay - ValuePay - FavorMoney - AllReturn - ReturnFavor) > 0 and MemberId= '" + Tmember.Id + "'");
            dgvMemberOwed.DataSource = MemberOweds;

            SPACommon.SetDataGridStyle(dgvMemberOwed);
            var moenyLogic = new MoneyLogic();
            var moenyList = moenyLogic.SqlWhere<T_Account>(" 1=1").ToList();
            var list = (from v in moenyList where v.Name != "现金" select v.Name).ToList();
            cmbBankAccountId.DataSource = list;
        }
        private void pbClose_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {

        }

        private void dgvMemberOwed_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvMemberOwed.EndEdit(DataGridViewDataErrorContexts.Commit);
            var select = MemberOweds.Where(m => m.Select == "是");
            txtNeedPay.Text = select.Sum(m => m.LastOwed).ToString();
            nudCashPay.Value = select.Sum(m => m.LastOwed);
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            TblMemberReturnMoney.AdminId = S_Admin.Id;
            TblMemberReturnMoney.MemberId = Tmember.Id;
            TblMemberReturnMoney.ShopId = S_Admin.ShopId;
            TblMemberReturnMoney.BankAccountId = cmbBankAccountId.ValueMember;
            TblMemberReturnMoney.BankPay = nudBankPay.Value;
            TblMemberReturnMoney.CashPay = nudCashPay.Value;
            TblMemberReturnMoney.FavorMoney = nudFavorMoney.Value;
            TblMemberReturnMoney.ReturnMoney = TblMemberReturnMoney.CashPay + 
                                               TblMemberReturnMoney.BankPay +
                                               TblMemberReturnMoney.FavorMoney;
            TblMemberReturnMoney.AddTime = dtpAddTime.Value;

            var returnMoney = TblMemberReturnMoney.ReturnMoney;
            var select = MemberOweds.Where(m => m.Select == "是");
            foreach (var item in select)
            {
                if (returnMoney == 0) break;
                var tblMemberReturnMoneyItem = new T_MemberReturnMoneyItem();
                tblMemberReturnMoneyItem.ObjectId = item.Id;
                if (returnMoney - item.LastOwed >= 0)
                {
                    returnMoney = returnMoney - item.LastOwed;
                }
                else
                {
                    item.LastOwed = returnMoney;
                    returnMoney = 0;
                }
                tblMemberReturnMoneyItem.ReturnMoney = item.LastOwed;
                TblMemberReturnMoneyItems.Add(tblMemberReturnMoneyItem);
            }
            int row = LgcMember.ReturnMoney(TblMemberReturnMoney, TblMemberReturnMoneyItems);
            const string message = "保存失败";
            if (row == 0) FormSysMessage.ShowSuccessMsg(message);
            else
            {
                MemberList.Instance.btnSearchDinner_Click(null, null);
                MemberList.Instance.btnSearch_Click(null, null);
            }
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();

        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void btnSearch_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }

        private void PnlInfoTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlInfoTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }

        private void PnlInfoTitle_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }
    }
}
